#ifndef _STRAWGUI_PRESET_GROUP_H_
#define _STRAWGUI_PRESET_GROUP_H_

#include "sLinkTo.h"
#include "sGui.h"
#include "sResourcePreset.h"
#include "sDrawable.h"

namespace sgui
{
namespace preset
{
	template < typename T >
	class sGroup : public sGUIObject
		, public obs::sObserver
#ifdef _EDITOR
		, public rtti::sObject
#endif
	{
	private:
		sLinkTo<T>* m_states[inst::CS_COUNT];
	public:
		void load(const db::Record &file)
		{
			sGUIObject::load(file);

			if (file.getClass() == db::RC_ARRAY)
			{
				for (u32 i = 0; i < inst::CS_COUNT; ++i)
					(*m_states)[i].load(file[i]);
			}
		}

		T* get(const inst::EControlState& state)
		{
			if (state != inst::CS_NORMAL && !(*m_states)[state].get())
			{
				return getNormal().get();
			}
			return (*m_states)[state].get();
		}

		const T* get(const inst::EControlState& state) const
		{
			if (state != inst::CS_NORMAL && !(*m_states)[state].get())
			{
				return getNormal().get();
			}
			return (*m_states)[state].get();
		}

		sLinkTo<T>& getNormal()
		{
			return (*m_states)[inst::CS_NORMAL];
		}

		sLinkTo<T>& getHover()
		{
			return (*m_states)[inst::CS_HOVER];
		}

		sLinkTo<T>& getActive()
		{
			return (*m_states)[inst::CS_ACTIVE];
		}

		const sLinkTo<T>& getNormal() const
		{
			return (*m_states)[inst::CS_NORMAL];
		}

		const sLinkTo<T>& getHover() const
		{
			return (*m_states)[inst::CS_HOVER];
		}

		const sLinkTo<T>& getActive() const
		{
			return (*m_states)[inst::CS_ACTIVE];
		}

		void save(db::Record &file) const
		{
			file.setSize(3);
			
			for (u32 i = 0; i < inst::CS_COUNT; ++i)
				(*m_states)[i].save(file[i]);
		}

		void clear()
		{
			for (u32 i = 0; i < inst::CS_COUNT; ++i)
				(*m_states)[i].clear();
		}

#ifdef _EDITOR
		void exportRTTI(rtti::sInfoList& list)
		{
			//
		}
#endif

		bool OnEvent(const obs::sEvent& event)
		{
			return false;
		}

		sGroup(sGUI& gui) :
			sGUIObject(gui)
		{
			for (u32 i = 0; i < inst::CS_COUNT; ++i)
				m_states[i] = new sLinkTo<T>(gui);
		}
			
		~sGroup()
		{
			for (u32 i = 0; i < inst::CS_COUNT; ++i)
				delete m_states[i];
		}
	};

};
};

#endif